package com.army.study.component;

import com.army.study.ResourceTable;
import com.army.study.RightAbility;
import com.army.study.databean.Item;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.agp.components.*;
import ohos.app.Context;
import ohos.bundle.ElementName;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

/**
 * 列表适配器类
 */
public class ListAdapter extends BaseItemProvider {

    // 上下文
    private static Context context;
    // 数据项列表
    private List<Item> items;
    // 自定义事件处理
    private MyEventHandle myEventHandle;


    /**
     * 构造方法
     * @param items
     * @param context
     */
    public ListAdapter(List<Item> items, Context context) {
        this.items = items == null ? new ArrayList() : items;
        this.context = context;
    }

    /**
     * 获取列表有多少项
     * @return
     */
    @Override
    public int getCount() {
        return items == null ? 0 : items.size();
    }

    /**
     * 获取当前项数据
     * @param i
     * @return
     */
    @Override
    public Item getItem(int i) {
        return this.items.get(i);
    }

    /**
     * 获取当前Id
     * @param i
     * @return
     */
    @Override
    public long getItemId(int i) {
        return i;
    }

    /**
     * 获取组件内容
     * @param i
     * @param component
     * @param componentContainer
     * @return
     */
    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        ViewHolder viewHolder = null;
        Component cmp = component;
        if (cmp == null) {
            cmp = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_item, null, false);
            // 初始化项布局
            viewHolder = new ViewHolder();
            // 获取图片组件
            viewHolder.img = (Image)cmp.findComponentById(ResourceTable.Id_img);
            // 获取标题组件
            viewHolder.title = (Text)cmp.findComponentById(ResourceTable.Id_title);
            // 缓存起来
            cmp.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) cmp.getTag();
        }

        viewHolder.img.setPixelMap(items.get(i).getImg());
        viewHolder.title.setText(items.get(i).getTitle());
        viewHolder.img.setClickedListener(listener -> itemClick(i));
        viewHolder.title.setClickedListener(listener -> itemClick(i));

        return cmp;
    }

    public void replace(Collection<Item> listConstructor) {
        if (listConstructor == null) {
            return;
        }
        this.items = null;
        // 重新初始化listContainer中的数据
        this.items = new ArrayList<>(0);
        // 将重新得到的项数据放到listContainer中
        this.items.addAll(listConstructor);
        // 刷新listContainer，调用getComponent方法重新设置页面元素布局
        notifyDataChanged();
    }


    /**
     * 内部类，封装列表项组件
     */
    private static class ViewHolder {
        // 显示图片
        Image img;
        // 显示标题
        Text title;
    }

    /**
     * 点击图片或标题事件
     * @param index
     */
    private void itemClick(int index) {
        // 初始化处理程序
        initHandler();
        // 获取内部事件
        InnerEvent event = InnerEvent.get(1, 0, index);
        myEventHandle.sendEvent(event);
    }

    /**
     * 初始化处理程序
     */
    private void initHandler() {
        EventRunner runner = EventRunner.getMainEventRunner();
        if (runner == null) {
            return;
        }
        myEventHandle = new MyEventHandle(runner);
    }

    /**
     * 自定义处理事件
     */
    public static class MyEventHandle extends EventHandler {
        MyEventHandle(EventRunner runner) throws IllegalArgumentException {
            super(runner);
        }

        @Override
        protected void processEvent(InnerEvent event) {
            super.processEvent(event);
            int eventId = event.eventId;
            int index = (Integer) event.object;
            if (eventId == 1) {
                IntentParams intentParams = new IntentParams();
                intentParams.setParam("index", index); // 选择列表项下标
                // 跳转到 RightAbility 分屏
                Intent intent = new Intent();
                intent.setParams(intentParams);
                ElementName element = new ElementName("", context.getBundleName(), RightAbility.class.getName());
                intent.setElement(element);
                context.startAbility(intent, 0);
            }
        }
    }
}
